#include "WINApt_Hdr.h"

BOOL bIsRun = FALSE;
XLOG xhLog = NULL;
XNETHANDLE xhPool = 0;
XNETHANDLE xhTCPPacket = 0;
XNETHANDLE xhTCPNet = 0;
HANDLE hTCPThread = 0;
WINAPT_SERVERCONFIG st_ServiceCfg;

void ServiceApp_Stop(int signo)
{
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN,_T("服务器退出..."));

    bIsRun = FALSE;

    NetCore_TCPEPoll_DestroyEx(xhTCPNet);
    ManagePool_Thread_Destroy(xhPool);
    HelpComponents_Datas_CloseEventEx(xhTCPPacket);
    HelpComponents_Datas_Destory(xhTCPPacket);
    LogicSql_Close();
    HelpComponents_XLog_Destroy(xhLog);
    exit(0);
}

int main(int argc,char **argv)
{
    bIsRun = TRUE;
    LPCTSTR lpszLogFile = _T("./WINApt_Log/WINApt.Log");
    HELPCOMPONENTS_XLOG_CONFIGURE st_XLogConfig;

    memset(&st_XLogConfig,'\0',sizeof(HELPCOMPONENTS_XLOG_CONFIGURE));
    memset(&st_ServiceCfg,'\0',sizeof(WINAPT_SERVERCONFIG));

    st_XLogConfig.XLog_MaxBackupFile = 10;
    st_XLogConfig.XLog_MaxSize = 1024000;
    _tcscpy(st_XLogConfig.tszFileName, lpszLogFile);

    if (!WINApt_Service_Parament(argc,argv,&st_ServiceCfg))
    {
        return -1;
    }

    signal(SIGINT,ServiceApp_Stop);

    xhLog = HelpComponents_XLog_Init(HELPCOMPONENTS_XLOG_OUTTYPE_STD | HELPCOMPONENTS_XLOG_OUTTYPE_FILE,&st_XLogConfig);
    if (NULL == xhLog)
    {
        printf("启动服务器失败，启动日志失败，错误：%lX",XLog_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务中，初始化日志系统成功"));

    if (st_ServiceCfg.nDeamon)
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("不支持守护进程..."));
        goto NETSERVICEEXIT;
    }

    if (!ManagePool_Thread_Create(&xhPool,st_ServiceCfg.Threads))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务器中，创建业务线程池失败，错误：%lX"),ManagePool_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务中，创建业务线程池成功！"));

    if (!HelpComponents_Datas_Init(&xhTCPPacket))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务器中，初始化客户端包管理器失败，错误：%lX"),Packets_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务器中，初始化客户端包管理器成功"));
    if (!HelpComponents_Datas_InitEventEx(xhTCPPacket))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务器中，初始化客户端包事件管理器失败，错误：%lX"),Packets_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务器中，初始化客户端包事件管理器成功"));

    if (!LogicSql_Init(st_ServiceCfg.st_SQLInfo.tszSQLAddr,st_ServiceCfg.st_SQLInfo.nSQLPort,st_ServiceCfg.st_SQLInfo.tszSQLUser,st_ServiceCfg.st_SQLInfo.tszSQLPass))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务中，启动数据库服务失败，错误：%lX..."),LogicSql_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务中，启动数据库服务成功。。。"));

    if (!NetCore_TCPEPoll_StartEx(&xhTCPNet,st_ServiceCfg.nPort))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务中，启动网络服务失败，错误：%lX..."),NetCore_GetLastError());
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务中，启动网络服务成功，端口：%d，句柄：%lld。。。"),st_ServiceCfg.nPort,xhTCPNet);
    NetCore_TCPEPoll_RegisterCallBackEx(xhTCPNet,WINApt_Callback_Login,WINApt_Callback_Recv,WINApt_Callback_Leave);
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务中，注册客户端网络服务事件成功！"));

    if (0 != pthread_create(&hTCPThread,NULL,WINApt_Thread_Packet,NULL))
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("启动服务器中，启动处理线程失败，错误：%d"),errno);
        goto NETSERVICEEXIT;
    }
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("启动服务器中，启动处理线程成功"));

    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("所有服务成功启动，服务运行中。。。"));
    while (bIsRun)
    {
        sleep(1);
    }

NETSERVICEEXIT:
    XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("有服务启动失败，服务器退出..."));
    bIsRun = FALSE;

    NetCore_TCPEPoll_DestroyEx(xhTCPNet);
    ManagePool_Thread_Destroy(xhPool);
    HelpComponents_Datas_CloseEventEx(xhTCPPacket);
    HelpComponents_Datas_Destory(xhTCPPacket);
    LogicSql_Close();
    HelpComponents_XLog_Destroy(xhLog);
    return 0;
}
